/**
 * @author wangmao
 * @date 2021/11/8 09:59
 */
namespace Validation2 {
    export interface StringValidator {
        isAcceptable(s: string): boolean;
    }
    let lettersRegexp = /^[A-Za-z]+$/;
    let numberRegexp = /^[0-9]+$/;
    export class LettersOnlyValidator implements StringValidator {
        public isAcceptable(s: string) {
            return lettersRegexp.test(s);
        }
    }

    export class ZipCodeValidator implements StringValidator {
        public isAcceptable(s: string) {
            return s.length === 5 && numberRegexp.test(s);
        }
    }
}
// Some samples to try
let stringsa = ["Hello", "98052", "101"];

// Validators to use
let validatorsa: { [s: string]: Validation2.StringValidator; } = {};
validatorsa["ZIP code"] = new Validation2.ZipCodeValidator();
validatorsa["Letters only"] = new Validation2.LettersOnlyValidator();

// Show whether each string passed each validator
for (let s of stringsa) {
    for (let name in validatorsa) {
        if (!validatorsa.hasOwnProperty(name)) continue
        let isMatch = validatorsa[name].isAcceptable(s);
        console.log(`'${ s }' ${ isMatch ? "matches" : "does not match" } '${ name }'.`);
    }
}
